package Frame.shopSet;

import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import VWorldGameStudio.FCPlayerInfo;
import VWorldGameStudio.FCPropulsion;
import VWorldGameStudio.FCRPlayerLink;
import VWorldGameStudio.FCTank;
import Function.FCMutilPlayCache;

import java.util.Vector;

import static FCRendering.FCRMaster.*;

public class FunctionXiCheShop extends AShopFuntion {
    private static final int DaLaPrice = 300;//打蜡去酸
    private static final int FuckShitPrice = 100;//铲一个粪多少G
    private Vector<FixTank> needServiceNames = new Vector<>();

    private int page = 0;//0首页，1 打蜡，2 清洗
    private FixTank selectFixTank = null;

    public FunctionXiCheShop() {
        buildShopWindow();
    }

    public void buildShopWindow() {
        FCTank tank = tankLib.getTankOfID(player1.getPlayertank1(), player1.getPlayertankHostId());
        if (tank != null) {
            needServiceNames.add(new FixTank(tank, langLib.getNameOfId(tank.getName())));
        }

        //这里帮了别人的坦克修理完了,先限制
        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
            if (!link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                continue;
            }
            FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
            if (p != null) {
                tank = tankLib.getTankOfID(p.getPlayertank1(), p.getPlayertankHostId());
                if (tank != null) {
                    needServiceNames.add(new FixTank(tank, langLib.getNameOfId(tank.getName())));
                }
            }
        }
        //还有牵引的
        for (FCPropulsion fc : fcv.getPropulsions()) {
            if (fc.getPropulsionHostId().equals(gameSave.HOST_UID) && fc.getPropulsionType().equals("tank")) {
                needServiceNames.add(new FixTank((FCTank) fc, langLib.getNameOfId(((FCTank) fc).getName())));
            }
        }
        resetUI();
    }

    private void resetUI() {
        FCRValues.resetUnifySayUIEntity();
        if (page == 0) {
            FCRValues.leftWindowUI[0].leftText = "打蜡除酸";
            FCRValues.leftWindowUI[1].leftText = "清洗除粪";
            FCRValues.rightWindowUI[6].rightText = "退出";
        } else if (page == 1) {
            FCRValues.rightWindowUI[0].leftText = "选择要打蜡的战车";
            int m = 1;
            for (int i = 0; i < needServiceNames.size(); i++) {
                FCRValues.leftWindowUI[m].leftText = needServiceNames.get(i).text;
                FCRValues.leftWindowUI[m].rightText = DaLaPrice + "G";
                m++;
            }
            if (selectFixTank != null) {
                FCRValues.rightWindowUI[0].leftText = "选择了" + selectFixTank.text;
            }
            FCRValues.rightWindowUI[5].rightText = "开始打蜡";
            FCRValues.rightWindowUI[6].rightText = "返回";
        } else if (page == 2) {
            FCRValues.rightWindowUI[0].leftText = "选择要清洗的战车";
            int m = 1;
            for (int i = 0; i < needServiceNames.size(); i++) {
                FixTank fixTank = needServiceNames.get(i);
                FCRValues.leftWindowUI[m].leftText = fixTank.text;
                FCRValues.leftWindowUI[m].rightText = Math.max(FuckShitPrice, fixTank.tank.getResidues().size() * FuckShitPrice) + "G";
                m++;
            }
            if (selectFixTank != null) {
                FCRValues.rightWindowUI[0].leftText = "选择了" + selectFixTank.text;
            }
            FCRValues.rightWindowUI[5].rightText = "开始清洗";
            FCRValues.rightWindowUI[6].rightText = "返回";
        }
    }

    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        if (entity.leftText.length() > 1) {
            if (page == 0) {
                page = i + 1;
                resetUI();
            } else if (page == 1 || page == 2) {
                if (i > 0) {
                    selectFixTank = needServiceNames.get(i - 1);
                    resetUI();
                }
            }
        }
    }

    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        switch (i) {
            case 5:
                if (page == 1 && selectFixTank != null) {
                    if (player1.getMoney() >= DaLaPrice) {
                        player1.setMoney(player1.getMoney() - DaLaPrice);
                        selectFixTank.tank.setAcidState(false);
                        String txt = "已完成对" + selectFixTank.text+"除酸打蜡，请继续";
                        selectFixTank = null;
                        resetUI();
                        FCRValues.rightWindowUI[0].leftText = txt;
                    }else {
                        FCRValues.rightWindowUI[1].leftText = "抱歉，金额不足！";
                    }
                } else if (page == 2 && selectFixTank != null) {
                    int needMoney = Math.max(FuckShitPrice, selectFixTank.tank.getResidues().size() * FuckShitPrice);
                    if (player1.getMoney() >= needMoney) {
                        player1.setMoney(player1.getMoney() - needMoney);
                        selectFixTank.tank.getResidues().clear();
                        for(int n = 0; n < 10; n++){
                            String tankI = selectFixTank.tank.getKnapsackItemIDOfNum(n);
                            if(tankI.contains(FCRConstants.BridShitName) || tankI.contains(FCRConstants.QianZhuName)){
                                selectFixTank.tank.insertItemToKnapsack(n, "null");
                            }
                        }
                        selectFixTank.tank.reInitShuxing();
                        String txt = "已完成对" + selectFixTank.text+"清洗，请继续";
                        selectFixTank = null;
                        resetUI();
                        FCRValues.rightWindowUI[0].leftText = txt;
                    }else {
                        FCRValues.rightWindowUI[1].leftText = "抱歉，金额不足！";
                    }
                }
                break;
            case 6:
                if (page == 0) {
                    fcv.setOpenSayShop(false);
                    FCRMaster.fcv.setOpenSay(false);
                    FCRMaster.fcv.setSayNpcName(null);
                    FCRMaster.fcv.setSayNpcTextNum(0);
                } else if (page == 1 || page == 2) {
                    page = 0;
                    selectFixTank = null;
                    resetUI();
                }
                break;
        }
    }
}
